package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.WebServiceInfoRequestor;

/**
 * Servlet implementation class GetWebServiceInfo
 */
public class GetWebServiceInfo extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	private static String amount;
	private static String epn;
	private static String bn;
	private static String iban;
	private static String bban;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetWebServiceInfo() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String clientTransactionID = request.getParameter("clientTransactioId");
		amount = request.getParameter("webs_amount");
		String type = request.getParameter("type");// indicate which type of web service to request for
		String accountID = request.getParameter("accountId");
		

		if (clientTransactionID == null
				|| clientTransactionID.trim().length() == 0 || amount == null
				|| amount.trim().length() == 0) {
//			System.out
//					.println("Client transaction id or amount format is illegal, redirect to the error page!");
			return;
		}
		
		WebServiceInfoRequestor.clearValues();	// before each web service request, clear the previous epn, bn, amount, bban and iban values
		if(type.equals("TemplateA")) {
			WebServiceInfoRequestor.requestOldWebService(accountID, clientTransactionID, amount);	// invoke the old web service request
			amount = WebServiceInfoRequestor.getAmount();
			epn = WebServiceInfoRequestor.getEpn();
			bn = WebServiceInfoRequestor.getBn();
			
//			System.out.println("TemplateA: GetWebServiceInfo Servlet: amount,epn,bn values: " + amount + "\t" + epn + "\t" + bn);
		}
		else if(type.equals("TemplateB")) {
			WebServiceInfoRequestor.requestBbanWebService(accountID, clientTransactionID, amount);
			amount = WebServiceInfoRequestor.getAmount();
			bn = WebServiceInfoRequestor.getBn();
			epn = WebServiceInfoRequestor.getEpn();
			bban = WebServiceInfoRequestor.getBBan();
			
//			System.out.println("TemplateB: GetWebServiceInfo Servlet: amount,epn,bn,bban values: " + amount + "\t" + epn + "\t" + bn +"\t" + bban);
		}
		else if(type.equals("TemplateC")) {
			WebServiceInfoRequestor.requestIbanWebService(accountID, clientTransactionID, amount);
			amount = WebServiceInfoRequestor.getAmount();
			bn = WebServiceInfoRequestor.getBn();
			epn = WebServiceInfoRequestor.getEpn();
			iban = WebServiceInfoRequestor.getIBan();
			
//			System.out.println("TemplateC: GetWebServiceInfo Servlet: amount,epn,bn,iban values: " + amount + "\t" + epn + "\t" + bn +"\t" + iban);
		}

		PrintWriter out = response.getWriter();
		if (type.equals("TemplateA")) {
			if(amount==null || epn==null || bn==null) {
				out.println("amount=error&epn=error&bn=error");
			}
			else {
				out.println("amount=" + amount + "&epn=" + epn + "&bn=" + bn);
			}
			
			out.flush();
			out.close();
		} else if (type.equals("TemplateB")) {
			if(amount==null || epn==null || bn==null || bban==null) {
				out.println("amount=error&epn=error&bn=error&bban=error");
			}
			else {
				out.println("amount=" + amount + "&epn=" + epn + "&bn=" + bn
						+ "&bban=" + bban);
			}
			
			out.flush();
			out.close();
		} else if (type.equals("TemplateC")) {
			if(amount==null || epn==null || bn==null || iban==null) {
				out.println("amount=error&epn=error&bn=error&iban=error");
			}
			else {
				out.println("amount=" + amount + "&epn=" + epn + "&bn=" + bn
						+ "&iban=" + iban);
			}
			
			out.flush();
			out.close();
		}

	}

}
